<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>利用闭包根据字段排序商品</title>
</head>
<body>
    <script>
        let lessons = [
            {
                name : "html",
                price : 99,
                click : 444
            },
            {
                name : "css",
                price : 89,
                click : 345
            },
            {
                name : "js",
                price : 109,
                click : 656
            }
        ];

        // let pr = lessons.sort((a,b) => {
        //     return a.price > b.price ? 1 : -1;
        // });
        // let cl = lessons.sort((a,b) => {
        //     return a.click > b.click ? 1 : -1;
        // });
        // console.table(cl);


        // 使用闭包
        function order(param,type = 'asc'){
            return lessons.sort((a,b) => {
                if(type == 'asc'){
                    return a[param] > b[param] ? 1 : -1;
                }
                return a[param] > b[param] ? -1 : 1;
            });
        };
        console.table(order('price'));
        console.table(order('click','desc'));
    </script>
</body>
</html>